<extend name="Base/common"/>
<block name="style">
    <style type="text/css">
        .weui-cell_access .weui-cell__ftnone:after{border-style:none;}
    </style>
</block>
<block name="body">
    <php>
        $PROVINCE =   C('PROVINCE');
    </php>
    <php>
        if(!empty($info)&&!empty($realname)){
            

    </php>

        <div style="margin:auto;margin-top:3%;width:94%;height:18%;background-image:url('__IMG__/{$info['openbank']}.png'); background-repeat:no-repeat; background-size:100% 100%;-moz-background-size:100% 100%;position:relative;">
            <p style="font-size:16px;color:#fff;height:20px;line-height:55px;margin-left:20%;">{$bank_name}</p>
            

                <p style="font-size:12px;color:#fff;height:20px;line-height:55px;margin-left:20%;">储蓄卡</p>
                <input type="hidden" id="card_number" value="{$info['card_number']}">
                <php>
                    $info['card_number']=substr($info['card_number'],count($info['card_number'])-4);
                </php>
                <p style="font-size:20px;color:#fff;height:25px;line-height:55px;margin-left:20%;">**** **** **** {$info['card_number']}</p>

                <button type="button" id="jiechu" style="position:absolute;right:7%;top:15%;color:#cacaca;font-size:15px;background:none;border:none">解除绑定</button>
            
        </div>

    <php>
        }else{
    </php>
    <form class="formtijiao" action="{:U('')}" method="post">
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">银行卡号</label></div>
                <div class="weui-cell__bd">
                   <input type="number" pattern="[0-9]*" placeholder="请输入银行卡号" name="card_number" value="{$info.card_number}" class="weui-input card_number">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">开户名字</label></div>
                <div class="weui-cell__bd">
                    {$realname}<input type="hidden" name="name" value="{$realname}">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">预留号码</label></div>
                <div class="weui-cell__bd">
                    <div class="weui-cell__bd">
                         {$mobile}<input type="hidden" name="mobile" value="{$mobile}">
                     </div>

                </div>
            </div>

            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">开户银行</label></div>
                <div class="weui-cell__bd">
                    <div class="weui-cell__bd">
                         <span id="bank_name"></span>
                         <input type="hidden" name="bank_name" value="{$bank_name}">
                     </div>

                </div>
            </div>
    
            
        </div>
        <empty name="info">
        <div class="page__bd page__bd_spacing" style="padding-top: 0.5em;">
            <button class="login-btn tijiao"  type="button">确定</button>
        </div>
            <else/>
            <div class="page__bd page__bd_spacing" style="padding-top: 0.5em;">
                <button class="weui-btn weui-btn_primary" onclick="javascript:popup.alert('您无权操作，如信息有误，请联系客服');return false;" style="background-color: #999;" type="button">确定</button>
            </div>
        </empty>
    </form>
        <p class="login-tips">
            <em> &nbsp;&nbsp;&nbsp;&nbsp;温馨提示</em>：
            <br> &nbsp;&nbsp;&nbsp;&nbsp;1、认证成功后无法修改，必须是本人姓名银行卡；
            <br> &nbsp;&nbsp;&nbsp;&nbsp;2、银行卡不正确者，无法进行下一步借款；
            <br> &nbsp;&nbsp;&nbsp;&nbsp;3、如果银行卡有误，请联系客服修改!
        </p>

    <php>
        }
    </php>
    <php>
        $province = array();
        foreach($PROVINCE as $k=>$v){
        $province[]=['label'=>$v,'value'=>$k];
        }
    </php>
</block>
<block name="script">
    <script type="text/javascript" src="https://wap.lianlianpay.com/lib/llpay.min.js" charset="utf-8"></script>
    <script >

        var province,Url;
        province = <php>echo json_encode($province)</php>;
        Url      = "{:U('Ajax/getRegion')}";
        Zepto(function($){

            $('.card_number').change(function(){
                var card_number=$(this).val();
                if(luhnCheck(card_number)){
                    $.ajax({
                        type:'post',
                        data:{'card_no':card_number},
                        url:"{:U('cardbin')}",
                        // dateType:'json',
                        success:function(m){
                            $('#bank_name').html(m);
                            $('#bank_name').next().val(m);
                        }
                    })
                }
                
            })




            $('.province_select').on('click', function () {
                weui.picker(province, {
                    onChange: function (result) {
                    },
                    onConfirm: function (result) {
                        for (var i=0;i<province.length;i++){
                            if(province[i].value==result){
                                $('.province_text').html(province[i].label);
                                $('.province').val(result);
                            }
                        }
                    }
                });
            });
            $('.city_select').on('click', function () {
                var reside_province = $('.province').val();
                if(reside_province=='' || reside_province==0){
                    popup.alert('请选择开户省份');return false;
                }
                AjaxGet(Url,{pid:reside_province},function(data){

                    weui.picker(data, {
                        onChange: function (result) {
                        },
                        onConfirm: function (result) {
                            for (var i=0;i<data.length;i++){
                                if(data[i].value==result){
                                    $('.city_text').html(data[i].label);
                                    $('.city').val(result);
                                }
                            }
                        }
                    });
                });
            });

            //提交表单
            $('.tijiao').click(function(){
                var name,card_number,mobile;
                
                name        = $('.name').val();
                card_number = $('.card_number').val();
                mobile      = $('.mobile').val();

                
                if(name==''){
                    popup.alert('请输入开户名字');return false;
                }
                if(card_number==''){
                    popup.alert('请输入银行卡号');return false;
                }

                var datas=$('.formtijiao').serializeArray();
                
                $.ajax({
                    type:'post',
                    url:"{:U('yinhang')}",
                    data:datas,
                    async:false,
                    dataType:'json',
                    success:function(m){
                        // popup.alert(m)
                        if(m.status==true){
                            //stringifier 函数的作用跟 parse 相反， 用来将一个js对象转换为 JSON 文本。
                            new LLPay().setData(JSON.stringify(m.info)).signReq(function(data){
                                // popup.alert(data.ret_code+'//////'+data.ret_msg)
                                if(data.ret_code =='0000'){
                                    var datas=m.data;
                                    datas['no_agree']=data.no_agree;
                                    $.ajax({
                                        type:'post',
                                        url:"{:U('yinhang2')}",
                                        async:false,
                                        data:datas,
                                        // dataType:'json',
                                        success:function(m){
                                            if(m==1){
                                                confirm('恭喜您，绑卡成功！');
                                                setTimeout(location.href="{:U('mobile')}",1000);
                                                
                                            }else{
                                                popup.alert('未知错误，无法绑定银行卡')
                                            }
                                            
                                        }
                                    });
                                    

                                }else{
                                    popup.alert(data.ret_msg);
                                }
                                
                            });
                        }else{
                            popup.alert(m.info)
                        }
                    }
                });
            }) ;
        });

        $('#jiechu').click(function(){
            var card_number=$('#card_number').val();
            $.ajax({
                type:'post',
                url:"{:U('bankcardunbind')}",
                data:{'card_number':card_number},
                dataType:'json',
                success:function(m){
                    confirm(m.info);
                    setTimeout(location.reload(),1000);
                    
                }
            });
        });

        //银行卡号码检测
        function luhnCheck(bankno) {
            var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位（与luhn进行比较）
            var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
            var newArr = new Array();
            for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
                newArr.push(first15Num.substr(i, 1));
            }
            var arrJiShu = new Array(); //奇数位*2的积 <9
            var arrJiShu2 = new Array(); //奇数位*2的积 >9
            var arrOuShu = new Array(); //偶数位数组
            for (var j = 0; j < newArr.length; j++) {
                if ((j + 1) % 2 == 1) { //奇数位
                    if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
                    else arrJiShu2.push(parseInt(newArr[j]) * 2);
                } else //偶数位
                arrOuShu.push(newArr[j]);
            }

            var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
            var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
            for (var h = 0; h < arrJiShu2.length; h++) {
                jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
                jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
            }

            var sumJiShu = 0; //奇数位*2 < 9 的数组之和
            var sumOuShu = 0; //偶数位数组之和
            var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
            var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
            var sumTotal = 0;
            for (var m = 0; m < arrJiShu.length; m++) {
                sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
            }

            for (var n = 0; n < arrOuShu.length; n++) {
                sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
            }

            for (var p = 0; p < jishu_child1.length; p++) {
                sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
                sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
            }
            //计算总和
            sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);

            //计算luhn值
            var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
            var luhn = 10 - k;

            if (lastNum == luhn) {
                // popup.alert('正确的银行卡号！');
                // $("#banknoInfo").html("luhn验证通过");
                return true;
            } else {
                popup.alert('不是正确的银行卡号！');
                // $("#banknoInfo").html("银行卡号必须符合luhn校验");
                return false;
            }
        }

    </script>

</block>